package com.example.bookpage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Alex-Zhang on 2017/11/2.
 */

public class CircleText extends View {
    public static final String TAG = "CircleText";

    public static final String TEXT = "CircleText";
    private final Paint sPaint;
    private  Rect mRect;
    private TextPaint mPaint;
    private Paint paint;
    private RectF rect;
    private Path mPath;
    private int radius = 100;

    public CircleText(Context context) {
        this(context, null);
    }

    public CircleText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextSize(30);
        mPaint.setColor(Color.BLUE);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(2);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);

        sPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        sPaint.setColor(Color.BLACK);
        mPath = new Path();

    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float centerX = getWidth() / 2;
        float centerY = getHeight() / 2;
        mRect = new Rect(0,0,getWidth(),getHeight());

        rect = new RectF(centerX  - radius,centerY- radius , centerX + radius, centerY + radius);

        mPath.addArc(rect, 180,180);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // canvas.restore();

        canvas.drawTextOnPath(TEXT,mPath,0,0,mPaint);
        canvas.drawPath(mPath,paint);
        canvas.drawRect(rect,paint);
        canvas.drawRect(mRect,paint);

        mPath.rewind();
        mPath.addArc(rect,180,-180);
        canvas.drawTextOnPath(TEXT,mPath,0,10,mPaint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension(width, heightSize);
        } else if (MeasureSpec.AT_MOST == widthMode) {
            setMeasuredDimension(500, 300);
        } else {
            setMeasuredDimension(width, heightSize);
        }
    }
}
